package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.OrderDetailMapper;
import com.ktmall.ktmalluijava.mapper.OrderMapper;
import com.ktmall.ktmalluijava.pojo.Order;
import com.ktmall.ktmalluijava.pojo.OrderDetail;
import com.ktmall.ktmalluijava.service.OrderService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.vo.OrderDetailVO;
import com.ktmall.ktmalluijava.vo.OrderVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/20 上午10:02
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;
    /**
     * 获取分页对象
     *
     * @param orderSn
     * @param userId
     * @param orderState
     * @return
     */
    @Override
    public Page<OrderVO> getPage(String orderSn, Integer userId, Integer orderState , Integer pageNO, Integer pageSize) {
        Page<OrderVO> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(orderMapper.count(orderSn,userId,orderState));
        page.setPageNo(pageNO);
        int offset = (page.getPageNo()-1)* page.getPageSize();
        page.setData(orderMapper.select(orderSn,userId,orderState,offset,pageSize));
        return page;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public Order detail(Integer id) {
        return orderMapper.detail(id);
    }

    /**
     * 详情
     *
     * @param order
     * @return
     */
    @Override
    public int update(Order order) {
        return orderMapper.update(order);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer... id) {
        if(id.length==1){
            return orderMapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
           count += orderMapper.delete(integer);
        }
        return count;
    }

    /**
     * 获取订单详细信息
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderDetailVO orderDetail(Integer orderId) {
        OrderVO orderVO = orderMapper.findById(orderId);
        List<OrderDetail> goods = orderDetailMapper.selectList(orderId);
        return new OrderDetailVO(orderVO,goods);
    }
}
